package cn.example.zjgsu.service.Impl;

import cn.example.zjgsu.dao.*;
import cn.example.zjgsu.pojo.*;
import cn.example.zjgsu.pojo.result.DetailFoodRecord;
import cn.example.zjgsu.pojo.result.OneClickFoodRecord;
import cn.example.zjgsu.pojo.result.ResultBriefPlan;
import cn.example.zjgsu.service.PublishPageServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author xuejiahui
 * @date 2020/3/29 -15:57
 */
@Service
public class PublishPageServerImpl implements PublishPageServer {
    @Autowired
    FoodRecordMapper foodRecordMapper;
    @Autowired
    PlanMapper planMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    ShopMapper shopMapper;
    @Autowired
    CityMapper cityMapper;

    public static int i;

    @Override
    public int insertCommonFoodRecord(FoodRecord foodRecord, String userid,int cityid) {
        Date date = new Date(System.currentTimeMillis());
        String formatdate = new SimpleDateFormat("HH:mm:ss").format(date);
        foodRecord.setTime(formatdate);
        foodRecord.setPraisenumber(0);
        foodRecord.setUserid(userid);
        foodRecord.setCityid(cityid);
        foodRecord.setPraisenumber(0);
        return foodRecordMapper.insert(foodRecord);
    }

    @Override
    public String insertPlanFoodRecord(MultipartFile[] files, String title, String content, String userid, int planid, int cityid) throws IOException {
        if (files==null){
            Date date = new Date(System.currentTimeMillis());
            String formatdate = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss").format(date);
            String cityName=cityMapper.selectByPrimaryKey(cityid).getName();
            FoodRecord foodRecord=new FoodRecord();
            foodRecord.setPraisenumber(0);
            foodRecord.setTime(formatdate);
            foodRecord.setPlanid(planid);
            foodRecord.setUserid(userid);
            foodRecord.setCityid(cityid);
            foodRecord.setContent(content);
            foodRecord.setLabel(cityName+"美食");
            foodRecord.setTitle(title);
            foodRecord.setViewnumber(0);
            foodRecordMapper.insert(foodRecord);
            return "插入成功";
        }else{
            System.out.println("files 不是Null");
            String pictureurl="";
            //获取文件名 : file.getOriginalFilename();
            System.out.println("文件长度"+files.length);
            for (int i = 0; i <files.length ; i++) {
                System.out.println(files[i].getName());
                String uploadFileName = files[i].getOriginalFilename();
                //如果文件名为空，直接回到首页！  
                if ("".equals(uploadFileName)) {
                    return "上传的图片文件名不能为空";
                }
                System.out.println("上传文件名 : " + uploadFileName);
                //上传路径保存设置  
                String path = ResourceUtils.getURL("classpath:").getPath()+"static/comment/";
                //如果路径不存在，创建一个  
                File realPath = new File(path);
                if (!realPath.exists()) {
                    realPath.mkdir();
                }
                System.out.println("上传文件保存地址：" + realPath);
                InputStream is = files[i].getInputStream();
                //文件输入流  
                OutputStream os = new FileOutputStream(new File(realPath, uploadFileName)); //文件输出流
                //读取写出  
                int len = 0;
                byte[] buffer = new byte[1024];
                while ((len = is.read(buffer)) != -1) {
                    os.write(buffer, 0, len);
                    os.flush();
                }
                os.close();
                is.close();
                if (i==files.length-1){
                    pictureurl=pictureurl+"http://10.21.234.73:8080/comment/"+uploadFileName;
                }else{
                    pictureurl=pictureurl+"http://10.21.234.73:8080/comment/"+uploadFileName+";";
                }
            }
            Date date = new Date(System.currentTimeMillis());
            String formatdate = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss").format(date);
            String cityName=cityMapper.selectByPrimaryKey(cityid).getName();
            FoodRecord foodRecord=new FoodRecord();
            foodRecord.setPraisenumber(0);
            foodRecord.setTime(formatdate);
            foodRecord.setPlanid(planid);
            foodRecord.setUserid(userid);
            foodRecord.setCityid(cityid);
            foodRecord.setContent(content);
            foodRecord.setLabel(cityName+"美食");
            foodRecord.setPictures(pictureurl);
            foodRecord.setTitle(title);
            foodRecord.setViewnumber(0);
            foodRecordMapper.insert(foodRecord);
            return "插入成功";
        }
    }

    @Override
    public int delteFoodRecord(int foodrecordid) {
        return foodRecordMapper.deleteByPrimaryKey(foodrecordid);
    }

    @Override
    public DetailFoodRecord seltectDetailFoodRecordById(int foodrecordid) {
        DetailFoodRecord detailFoodRecord = new DetailFoodRecord();
        FoodRecord foodRecord = foodRecordMapper.selectByPrimaryKey(foodrecordid);
        UserBriefInformation userBriefInformation = userMapper.selectUserBriefByUserid(foodRecord.getUserid());
        ResultBriefPlan resultBriefPlan = new ResultBriefPlan();
        if (foodRecord.getPlanid()!=null){
            Plan plan = planMapper.selectPlanById(foodRecord.getPlanid());
            resultBriefPlan.setId(plan.getId());
            List<BriefPlanItem> briefPlanItems = new ArrayList<>();
            for (Planitem planitem : plan.getPlanitems()) {
                BriefPlanItem briefPlanItem = new BriefPlanItem();
                briefPlanItem.setFoodname(planitem.getFoodname());
                briefPlanItem.setFoodurl(planitem.getFoodurl());
                briefPlanItem.setShopname(planitem.getShopname());
                briefPlanItem.setKind(planitem.getKind());
                briefPlanItems.add(briefPlanItem);
            }
            resultBriefPlan.setBriefPlanItems(briefPlanItems);
        }
        detailFoodRecord.setContent(foodRecord.getContent());
        detailFoodRecord.setId(foodRecord.getId());
        detailFoodRecord.setLabel(foodRecord.getLabel());
        detailFoodRecord.setPraisenumber(foodRecord.getPraisenumber());
        detailFoodRecord.setTime(foodRecord.getTime());
        detailFoodRecord.setTitle(foodRecord.getTitle());
        detailFoodRecord.setPictures(Arrays.asList(foodRecord.getPictures().split(";")));
        detailFoodRecord.setResultBriefPlan(resultBriefPlan);
        detailFoodRecord.setUserBriefInformation(userBriefInformation);
        return detailFoodRecord;
    }

    @Override
    public int praiseFoodRecord(int foodrecordid) {
        new Thread(()->{
            synchronized (foodRecordMapper){
                i=0;
                i=foodRecordMapper.updatePraisenumberById(foodrecordid);
            }
        }).start();
        return i;
    }

    @Override
    public OneClickFoodRecord oneClickFoodRecord(int planid) {
        Plan plan = planMapper.selectPlanById(planid);
        String shopname=null;
        OneClickFoodRecord oneClickFoodRecord = new OneClickFoodRecord();
        plan.getPlanitems();
        ResultBriefPlan resultBriefPlan = new ResultBriefPlan();
        resultBriefPlan.setId(plan.getId());
        List<BriefPlanItem> briefPlanItems = new ArrayList<>();
        for (Planitem planitem : plan.getPlanitems()) {
            BriefPlanItem briefPlanItem = new BriefPlanItem();
            briefPlanItem.setFoodname(planitem.getFoodname());
            briefPlanItem.setFoodurl(planitem.getFoodurl());
            briefPlanItem.setShopname(planitem.getShopname());
            briefPlanItem.setKind(planitem.getKind());
            if (planitem.getKind()==1){
                shopname=planitem.getShopname();
            }
            briefPlanItems.add(briefPlanItem);
        }
        resultBriefPlan.setBriefPlanItems(briefPlanItems);
        String cityname = shopMapper.getCityNameByShopName(shopname);
        System.out.println(shopname);
        System.out.println(cityname);
        oneClickFoodRecord.setCityName(cityname);
        oneClickFoodRecord.setResultBriefPlan(resultBriefPlan);
        oneClickFoodRecord.setTitle(cityname+"欢迎你");
        return oneClickFoodRecord;
    }
}
